<?php
namespace Home\Service;
use Think\Controller;
// +----------------------------------------------------------------------
// | Date:2017年10月13日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 重置个人考勤数据
// +----------------------------------------------------------------------
class ResetDataService extends Controller{
    public $error = '';
    
    /**
     * 删除旧数据，重建个人当天的考勤数据
     * @param unknown $name
     * @param unknown $day_cur
     * date:2017年10月13日
     * author: EK_熊
     */
    public function reset_user_attday($name,$date){
        $attenDayModel = D('AttendanceDay');
        
        //删除旧的日常考勤数据
        $map_day['date'] = $date;
        $map_day['name'] = $name;
        $del = $attenDayModel->where($map_day)->delete();
        $ret = $attenDayModel->att_day_reload($date,$name);
        if(!$ret) {
            //$this->error($attenDayModel->getError());
            $this->error = $attenDayModel->getError();
        }
        return $ret;
    }
    
    /**
     * 重置个人，当天的申请数据
     * 
     * 返回输出:已处理的申请记录id，必须重复处理
     * @param unknown $name
     * @param unknown $date
     * @param array $deal_id    如果是处理大于1天的申请记录，需要传入该值，避免重复处理申请数据
     * date:2017年10月13日
     * author: EK_熊
     */
    public function reset_user_apply($name,$date,$deal_id=array()){
        $applyModel = D('Apply');
        $approverModel =D('Approver');
        
        $map_apply['_string']="(`type`=4 AND DATE_FORMAT(end_time,'%Y-%m-%d')='$date') OR (DATE_FORMAT(start_time,'%Y-%m-%d') = '$date') OR (DATE_FORMAT(start_time,'%Y-%m-%d') <= '$date' AND DATE_FORMAT(end_time,'%Y-%m-%d') >= '$date')";
        $map_apply['status'] = 1;//必须是审核通过的数据才需要重新
        if ($deal_id) $map_apply['id'] = ['not in',$deal_id];
        if ($name) $map_apply['name'] = $name;
        
        $ids = $applyModel->where($map_apply)->group('CONCAT(start_time,end_time)')->getField('id',true);//使用group+concat组合去除重复记录
        //返回信息
        $data['msg']="";
        if ($ids) {
            foreach ($ids as $appid){
                $ret = $approverModel->_action_pass($appid);
                $errMsg[$appid] = "[{$appid}]申请数据重载完成！！";
                if (!$ret){
                    $errMsg[$appid] = "[$appid]错误：".$approverModel->getError();
                }
                $deal_id[]=$appid;
            }
            if ($errMsg){
                $errMsg = implode('&#13;&#10;',$errMsg);
            }
            $data['msg']=$errMsg;
        }
        $data['deal_id'] = implode(',',$deal_id);//返回带出去给前端的已处理完毕的数据id，再让前端带进来        
        return $data;
    }
    
}